package cn.maxwell.user.controller;

import cn.maxwell.basic.util.JsonResult;
import cn.maxwell.basic.util.PageList;
import cn.maxwell.user.domain.Logininfo;
import cn.maxwell.user.query.LogininfoQuery;
import cn.maxwell.user.service.ILogininfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping //get请求
    @ApiOperation(value = "查询所有")
    public List<Logininfo> loadAll() {
        return logininfoService.loadAll();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}") //get请求，携带id参数
    @ApiOperation(value = "通过ID查询")
    //swagger注解：paramType="path"：解析path路径携带的参数；dataType="Long"：指定参数类型
    @ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "path", dataType = "Long")
    public Logininfo loadById(@PathVariable("id") Long id) {//@PathVariable：获取path路径携带的参数
        return logininfoService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @PathVariable:从发送的请求路径中拿到数据
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除操作")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            logininfoService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 添加或修改
     *
     * @param logininfo
     * @return
     * @RequestBody：从请求体中拿到数据
     */
    @PutMapping()
    @ApiOperation(value = "添加或修改操作")
    public JsonResult save(@RequestBody Logininfo logininfo) {//@RequestBody:在请求体中获取参数
        try {
            if (logininfo.getId() == null) {
                logininfoService.add(logininfo);
            } else {
                logininfoService.update(logininfo);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     *
     * @param logininfoQuery
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "分页查询")
    public PageList<Logininfo> query(@RequestBody LogininfoQuery logininfoQuery) {
        return logininfoService.queryPage(logininfoQuery);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public JsonResult patchDelete(@RequestBody List<Long> ids) { //List<Long>:要删除的id的集合
        try {
            logininfoService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
}
